﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using FineUI;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Org;
using ListItem = System.Web.UI.WebControls.ListItem;

namespace ZHDJ.BranchCenter.Module.OrganizationalLife
{
    public partial class MemberCostBase : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyMemberCostAdd";
            }
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string LoginID
        {
            get { return GetQueryIntValue("LoginID"); }
        }
        #endregion

        #region 页面属性
        public string EditorContent
        {
            get; set;
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }

        private void LoadData()
        {

            if (string.IsNullOrEmpty(LoginID))
            {
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            this.txtName.Text = DB.PartyOrgMembers.Find(LoginID).UserName;
            txtCost.Text= PartyOrgMemberSetting.GetSettingValue(LoginID, "MemberCostBase");


        }
        #endregion

        #region Events

        private void SaveData()
        {
           if (string.IsNullOrEmpty(LoginID))
            {
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            PartyOrgMemberSetting.SaveSettingValue(LoginID, "MemberCostBase", txtCost.Text);
            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }


        #endregion

        protected void ddlTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectvalue = ddlTemplate.SelectedValue;
            if (selectvalue == "0")
            {
                NumberBox1.Label = "职务工资（元）";
                NumberBox2.Label = "级别工资（元）";
                NumberBox3.Label = "工作性津贴（元）";
                NumberBox4.Label = "生活性津贴（元）";
                NumberBox5.Label = "养老保险（元）";
                NumberBox6.Label = "职业年金（元）";
                NumberBox7.Label = "医疗保险（元）";
                NumberBox8.Label = "个人所得税（元）";
                NumberBox7.Hidden = false;
                NumberBox8.Hidden = false;
            }
            else if (selectvalue == "1")
            {
                NumberBox1.Label = "岗位工资（元）";
                NumberBox2.Label = "薪级工资（元）";
                NumberBox3.Label = "基础性绩效工资（元）";
                NumberBox4.Label = "奖励性绩效工资（元）";
                NumberBox5.Label = "养老保险（元）";
                NumberBox6.Label = "职业年金（元）";
                NumberBox7.Label = "医疗保险（元）";
                NumberBox8.Label = "个人所得税（元）";
                NumberBox8.Label = "个人所得税（元）";
                NumberBox7.Hidden = false;
                NumberBox8.Hidden = false;
            }
            else {
                NumberBox1.Label = "基本工资（元）";
                NumberBox2.Label = "岗位工资（元）";
                NumberBox3.Label = "奖金（元）";
                NumberBox4.Label = "养老保险（元）";
                NumberBox5.Label = "职业年金（元）";
                NumberBox6.Label = "医疗保险（元）";
                NumberBox7.Hidden = true;
                NumberBox8.Hidden = true;
            }
        }

        protected void Calculate_Click(object sender, EventArgs e)
        {
            var selectvalue = ddlTemplate.SelectedValue;
            var costBase = 0.00;
            if (selectvalue == "2")
            {                costBase = Convert.ToDouble(NumberBox1.Text)
                   + Convert.ToDouble(NumberBox2.Text)
                   + Convert.ToDouble(NumberBox3.Text)
                   - Convert.ToDouble(NumberBox4.Text)
                   - Convert.ToDouble(NumberBox5.Text)
                   - Convert.ToDouble(NumberBox6.Text);
            }
            else
            {
                costBase = Convert.ToDouble(NumberBox1.Text)
                    + Convert.ToDouble(NumberBox2.Text)
                    + Convert.ToDouble(NumberBox3.Text)
                    + Convert.ToDouble(NumberBox4.Text)
                    - Convert.ToDouble(NumberBox5.Text)
                    - Convert.ToDouble(NumberBox6.Text)
                    - Convert.ToDouble(NumberBox7.Text)
                    - Convert.ToDouble(NumberBox8.Text);
            }
            txtCost.Text = GetPartyMemberShipdue(costBase).ToString();
        }

        private double GetPartyMemberShipdue(double costBase)
        {
            var due = 0.00;
            if (costBase <= 3000)
            {
                due = costBase * 0.005;
            }
            else if (costBase > 3000 && costBase <= 5000)
            {
                due = costBase * 0.01;
            }
            else {
                due = costBase * 0.015;
            }
            return due;
        }

    }
}